// @flow
import React, { Component } from 'react'
import styles from './LogSetting.css'
import { DatePicker, Button, Input } from 'antd'
import LogSettingGrid from './LogSettingGrid'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import moment from 'moment'

type Props = {
  history: Object
}
type States = {
  logs: Array<Object>,
  startDate: string,
  endDate: string,
  keyWord: string,
  total: number,
  current: number,
  pageSize: number
}
class LogSetting extends Component {
  props: Props
  state: States
  getLogSetting: Function
  addLogSetting: Function
  changeStartDate: Function
  changeEndDate: Function
  handleSearch: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      logs: [],
      total: 0,
      current: 1,
      pageSize: 20,
      keyWord: '',
      startDate: moment(0, 'HH').format('YYYY-MM-DDTHH:mm:ss'),
      endDate: moment().format('YYYY-MM-DDTHH:mm:ss')
    }
    this.getLogSetting = this.getLogSetting.bind(this)
    this.addLogSetting = this.addLogSetting.bind(this)
    this.changeStartDate = this.changeStartDate.bind(this)
    this.changeEndDate = this.changeEndDate.bind(this)
    this.handleSearch = this.handleSearch.bind(this)
  }
  handleSearch () {
    this.setState({
      current: 1
    }, () => {
      this.getLogSetting(this.state.current, this.state.pageSize)
    })
  }
  changeEndDate (date: any) {
    this.setState({
      endDate: moment(date).format('YYYY-MM-DDTHH:mm:ss')
    })
  }
  changeStartDate (date: any) {
    this.setState({
      startDate: moment(date).format('YYYY-MM-DDTHH:mm:ss')
    })
  }

  componentDidMount () {
    this.getLogSetting(this.state.current, this.state.pageSize)
  }

  getLogSetting (page: number, pageSize: number) {
    const { startDate, endDate, keyWord } = this.state
    universalFetch(`${__SERVICE_API__}sys/logs?pageNum=${page}&pageSize=${pageSize}&startDateTime=${startDate}&endDateTime=${endDate}&keyWord=${keyWord}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          current: page,
          total: json.pageInfo ? json.pageInfo.total : 1,
          logs: json.result
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }

  addLogSetting () {
    this.props.history.push('/business/customer-add')
  }
  getKeyWord = (e) => {
    this.setState({
      keyWord: e.target.value
    })
  }
  render () {
    const { logs, startDate, endDate, current, pageSize, total } = this.state
    return (
      <div className={styles['main']}>
        <div className={styles['search']}>
          <DatePicker showTime format='YYYY-MM-DD HH:mm:ss'
            style={{ width: 150 }} onChange={this.changeStartDate} value={moment(startDate)} />
          <div className={styles['mark']}>-</div>
          <DatePicker showTime format='YYYY-MM-DD HH:mm:ss'
            style={{ width: 150 }} onChange={this.changeEndDate} value={moment(endDate)} />
          <Input placeholder='关键字' style={{ width: 100, marginLeft: 10 }} onChange={this.getKeyWord} />
          <Button icon='search' onClick={this.handleSearch} style={{ margin: '2px 0 0 10px' }} />
        </div>
        <LogSettingGrid {...{
          current,
          pageSize,
          total,
          refreshLogSettings: this.getLogSetting,
          rowData: logs
        }} />
      </div>
    )
  }
}

export default LogSetting
